package com.fox.hiring.talent.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.fox.hiring.common.Constant;
import com.fox.hiring.common.PageModel;
import com.fox.hiring.common.util.AppUtil;
import com.fox.hiring.company.dao.ApplicationDao;
import com.fox.hiring.company.entity.ApplicationEntity;
import com.fox.hiring.talent.dao.ResumeDao;
import com.fox.hiring.talent.dao.TalentDao;
import com.fox.hiring.talent.entity.ResumeEntity;
import com.fox.hiring.talent.entity.TalentEntity;

/**
 * Talent Servlet
 * 
 * @author fox.
 * @since 2016年2月26日
 */
public class TalentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private TalentDao talentDao = new TalentDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置POST请求的编码
		request.setCharacterEncoding("UTF-8");
		// 根据命令参数，决定调用哪个操作方法
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("register".equals(command)) {
			register(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}
	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取参数；
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		// 2、调用业务逻辑；
		TalentEntity talent = talentDao.login(code, password);
		// 3、转向
		if (talent == null) {
			response.sendRedirect("view/talent/login.jsp?loginError=true");
		} else {
			request.getSession().setAttribute("talent", talent);
			response.sendRedirect("TalentServlet?command=main");
		}
	}

	/**
	 * 携带显示信息转到求职者主页
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 查询当前登录者所有的简历
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);

		// 查询当前登录者所有申请的职位
		int pageNo = 1;
		int pageSize = 10;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr != null) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		PageModel<ApplicationEntity> pageModel = applicationDao
				.listAllApplyInfo(talentId, pageNo, pageSize);
		request.setAttribute("pageModel", pageModel);

		// 登录成功时不传递操作结果标识
		String result = request.getParameter("result");
		if (result != null) {
			request.setAttribute("result", result);
		}

		request.getRequestDispatcher("view/talent/main.jsp").forward(request,
				response);
	}

	/**
	 * 注册
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取参数；
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbies = request.getParameterValues("hobby");
		TalentEntity talent = new TalentEntity();
		talent.setCode(code);
		talent.setPassword(password);
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		StringBuilder sb = new StringBuilder();
		if (hobbies != null) {
			// for (int i = 0; i < hobbies.length; i++) {
			// if (i == hobbies.length - 1) {
			// sb.append(hobbies[i]);
			// } else {
			// sb.append(hobbies[i] + "|");
			// }
			// }
			sb.append(StringUtils.join(hobbies, "|"));
		}
		talent.setHobby(sb.toString());

		// 2、调用业务逻辑；

		int row = talentDao.register(talent);
		// 3、转向
		if (row == 1) {
			response.sendRedirect("view/talent/login.jsp?registerSuccess=true");
		} else {
			request.setAttribute("talent", talent);
			request.getRequestDispatcher("view/talent/register.jsp").forward(
					request, response);
		}
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();
		response.sendRedirect("view/talent/login.jsp?sessionInvalidate=true");
	}

	/**
	 * 修改个人资料
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		// 1、获取参数；
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");

		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbies = request.getParameterValues("hobby");

		TalentEntity talent = new TalentEntity();
		talent.setId(talentEntity.getId());
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		if (hobbies != null) {
			talent.setHobby(StringUtils.join(hobbies, "|"));
		} else {
			talent.setHobby("");
		}

		// 2、逻辑业务逻辑；
		int row = talentDao.update(talent);
		// 3、转向
		if (row == 1) {
			// 更新session
			talent.setCode(talentEntity.getCode());
			request.getSession().setAttribute("talent", talent);
			response.sendRedirect("view/talent/talent-info.jsp?result="
					+ Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("view/talent/talent-info.jsp?result="
					+ Constant.OPERATE_ERROR);
		}
	}
}
